var btn = document.getElementById('btn2');
var clipboard = new ClipboardJS(btn);//实例化


function GetQueryString (name) { //获取指定name的url参数
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return (r[2]);
	return null;
}

var userId = GetQueryString('userId');//||'9381352'
var token = GetQueryString('token');//||'b8dec4ef57eb434d8953ca6e62766d08'
var utId = GetQueryString('utId');//||'123'
let imgUrl = '';
var host = 'http://'+ GetQueryString('invokeDomain');
 //复制成功执行的回调，可选
clipboard.on('success', function(e) {
	console.log(e);
	//复制成功 
	Toast('链接已复制！')
});
function Toast (txt){
	var toastHtml = '<div class="tk_info" id="tkInfo"></div>';
	var toast = $('body #tkInfo');
	if (toast.length) {
		toast.html(txt).show();
	}else{
		$('body').append(toastHtml);
		$('body #tkInfo').html(txt).show();
	}
	setTimeout(function(){
	  $('body #tkInfo').hide();
	},1500)
}
$(".saves").click(function(){
	window.open('http://127.0.0.1:2323/src/page/images/chesscardimg01.jpg')
})

 //上传图片
$('#input_file').on('change',function(event){
	if(window.FileReader) {
		var reader = new FileReader();
	} else {
		Toast("您的设备不支持图片预览功能，如需该功能请升级您的设备！");
		return false;
	}
	//获取文件
	var file = event.target.files[0];
	var imageType = /^image\//;
	//是否是图片
	if(!imageType.test(file.type)) {
		Toast("请选择图片！");
		return;
	}
	reader.readAsDataURL(file);
	//读取完成
	reader.onloadend = function(e) {
		//图片路径设置为读取的图片
		let formDatas = new FormData();
		formDatas.append('file', $('#input_file')[0].files[0]);
		var data = e.target.result; 
		var img = $(".sctp").find('img');
		img.attr('src',data);
	};
})

////system/api/appuser/taskCheckStatus?utId=123
var  btn = true;
$.ajax({
	type: "get",
	url: host+"/system/api/appuser/taskCheckStatus",
	data: {
		'utId':utId
	},
	success: function (d) {
		let code = d.code;
		let desc = d.msg||'网络错误';
		if (code === 701||code === 702 ) {
			btn = false;
			$("#submitBtn").css({"background":"url(./images/chesscard08.png) no-repeat","background-size":"100% 100%"})
		}else{
			//Toast(desc);
		}
	},
	error: function (data) {
		Toast('请求异常');
	},
})


//提交审核
$('#submitBtn').on('click',function(){
	if (!btn) {
		Toast('今天已提交过审核！');
		return;
	}
	let formDatas = new FormData();
	formDatas.append('file', $('#input_file')[0].files[0]);
	if (!formDatas) {
		Toast('请选择图片！');
		return;
	}
	//ajax请求
	$.ajax({
		type: "post",
		url: host+"/system/api/appuser/uploadImage",
		async: false, //默认是true：异步，false：同步。
		data: formDatas,
		dataType: 'json',
		processData: false, // 告诉jQuery不要去处理发送的数据
		contentType: false, // 告诉jQuery不要去设置Content-Type请求头
		xhrFields:{withCredentials:true},
		success: function (d) {
			let code = d.code;
			let desc = d.msg||'网络错误';
			if (code === 0 ) {
				Toast('上传成功');
				imgUrl =  d.data;
			}else{
				Toast(desc);
				return false;
			}
		},
		error: function (data) {
			Toast('请求异常');
		},
	});
	if(imgUrl){
		$.ajax({
			type: "get",
			url: host+"/system/api/appuser/submitTaskCheck",
			data: {
				'userId':9381352,
				'token':'b8dec4ef57eb434d8953ca6e62766d08',
				'utId':utId,
				'image':imgUrl,
			},
			success: function (d) {
				let code = d.code;
				let desc = d.msg||'网络错误';
				if (code === 0 ) {
					Toast(desc);
					$("#submitBtn").css({"background":"url(./images/chesscard08.png) no-repeat","background-size":"100% 100%"})
				}else{
					Toast(desc);
				}
			},
			error: function (data) {
				Toast('请求异常');
			},
		})
	}
	
});
function getNowTime() {
    var date = new Date();
    this.year = date.getFullYear();
    this.month = date.getMonth() + 1;
    this.date = date.getDate();
    this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
//  var currentTime = this.year+'-'+this.month + '-' + this.date + ' ' + this.hour + ':' + this.minute + ':' + this.second;
    var currentTime = this.hour + ':' + this.minute + ':' + this.second;
    if(this.hour==00&&this.minute==00&&this.second>0){
    	clearInterval(timer)
    	$("#submitBtn").css({"background":"url(./images/chesscard07.png) no-repeat","background-size":"100% 100%"})
    }else{
    	clearInterval(timer)
    	timer = setInterval(function(){ getNowTime() }, 1000);
    }
    return currentTime;
}
var timer = setInterval(function(){ getNowTime() }, 1000);
//返回app
$('.back').on('click',function(){
	H5brigeAPP.NativeBack()
    // window.webkit.messageHandlers.NativeBack.postMessage(null)
})